package com.controller;

import com.domain.Account;
import com.domain.DateTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping(path ="/users")
public class HelloController {
    @RequestMapping(path ="/hello")
    public String say(HttpServletResponse response ,HttpServletRequest request) throws IOException, ServletException {
        System.out.println("hello SpringMvc你好难找啊");
       // request.getRequestDispatcher(request.getContextPath()+"/date.jsp").forward(request,response);
       // response.sendRedirect(request.getContextPath()+"/date.jsp");
        System.out.println("请求转发后面的执行");

        return "success";
    }
    @RequestMapping(path ="/sayhello")
    public String sayhello(){
        System.out.println("你好，SpringMvc你干啥干啥");
        return "success";
    }
    @RequestMapping(value ="/paramsTest1")
    public String paramsTest1(String username,String password){
        System.out.println(username+" "+password);
        return "success";
    }
    @RequestMapping(path = "/paramsTest2",method={RequestMethod.POST} )
    public String paramsTest2(Account account){
        System.out.println(account);
        return  "success";
    }
    @RequestMapping(path = "/paramsDateSwap",method = {RequestMethod.POST})
    public String paramsDateSwap(HttpServletRequest request,DateTest date ){
        System.out.println(date);
        System.out.println(request.getParameter("birthday"));
        return "success";
    }
    /*获取Servlet中的API*/
    @RequestMapping(path = "/ServletAPI")
    public String servletAPI(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request);
        HttpSession session = request.getSession();
        ServletContext servletContext = session.getServletContext();
        System.out.println(response);
        System.out.println(servletContext);
        return "success";
    }
    /*用于测试请求转发与重定向的*/
    @RequestMapping(path ="/test")
    public void say1(HttpServletResponse response ,HttpServletRequest request) throws IOException, ServletException {
        System.out.println("hello SpringMvc你好难找啊");
       // request.getRequestDispatcher("/date.jsp").forward(request,response);
        response.sendRedirect(request.getContextPath()+"/params.jsp");
        System.out.println("请求转发后面的执行");
        request.getRequestDispatcher("/date.jsp").forward(request,response);

    }
}
